package com.wn.class30.tools;

import com.wn.class30.entity.User;
import com.wn.class30.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Component
public class UseridUtils {
    @Resource
    private UserService userService;
/*根据openid查询用户id*/
    public Integer queryUseridByOptenid(HttpServletRequest req) throws Exception{
        HttpSession session = req.getSession();
        Integer userid = (Integer) session.getAttribute("userid");
        if(userid==null){
            String openid = (String) session.getAttribute("openid");
            List<User> users = userService.queryByOpenid(openid);
            if(users==null ||users.size()==0){
                return null;
            }else{
                Integer userId = users.get(0).getUserId();
                session.setAttribute("userid",userId);
                return userId;
            }
        }
        return userid;
    }
}
